/*
* PermissionServiceImpl.java
* Created on  2014-3-14 下午3:15
* 版本       修改时间          作者      修改内容
* V1.0.1    2014-3-14       gaoxinyu    初始版本
*
*/
package com.genghis.ptas.security.service.impl;

import com.genghis.core.page.entity.Page;
import com.genghis.core.page.entity.PageParameter;
import com.genghis.ptas.security.dao.PermissionDao;
import com.genghis.ptas.security.entity.MenuPerm;
import com.genghis.ptas.security.entity.Permission;
import com.genghis.ptas.security.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 类的描述信息
 *
 * @author qinw
 * @version 1.0.1
 */
@Service("permissionService")
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;

    public List<MenuPerm> getMenuPermTree() {
        List<MenuPerm> menuPermList = new ArrayList<MenuPerm>();
        menuPermList.add(new MenuPerm(1, "", "权限选择", -1));

        menuPermList.add(new MenuPerm(66, "statistic", "统计管理", 1));
        menuPermList.add(new MenuPerm(67, "statistic_course", "课件统计", 66));
        menuPermList.add(new MenuPerm(68, "statistic_type", "分类统计", 66));

        menuPermList.add(new MenuPerm(3, "course", "课件管理", 1));
        menuPermList.add(new MenuPerm(13, "course_info", "课件信息管理", 3));
        menuPermList.add(new MenuPerm(14, "course_register", "注册课件管理", 3));

        menuPermList.add(new MenuPerm(4, "publicity_note", "笔记管理", 1));
        menuPermList.add(new MenuPerm(89, "publicity_note_list", "公示笔记列表", 4));

        menuPermList.add(new MenuPerm(75, "task", "试题管理", 1));
        menuPermList.add(new MenuPerm(76, "task_library", "试题库管理", 75));
        menuPermList.add(new MenuPerm(77, "task_examination", "试卷管理", 75));

        menuPermList.add(new MenuPerm(78, "classroom", "班级管理", 1));
        menuPermList.add(new MenuPerm(79, "classroom_info", "班级信息管理", 78));

        menuPermList.add(new MenuPerm(88, "question", "问题管理", 1));
        menuPermList.add(new MenuPerm(90, "question_list", "问题列表", 88));

        menuPermList.add(new MenuPerm(7, "system", "系统设置", 1));
        menuPermList.add(new MenuPerm(23, "system_user", "用户管理", 7));
        menuPermList.add(new MenuPerm(24, "system_role", "角色管理", 7));
        menuPermList.add(new MenuPerm(25, "system_permission", "权限管理", 7));
        menuPermList.add(new MenuPerm(26, "system_dict", "字典管理", 7));
        menuPermList.add(new MenuPerm(27, "system_type", "分类管理", 7));


        return menuPermList;
    }

    @Override
    public Page<Permission> listAllPermission(PageParameter pageParameter, Permission permission) {
        return permissionDao.listAllPermission(pageParameter, permission);
    }

    @Override
    public Boolean checkRepeatPermissionToken(String permissionToken, String oldPermissionToken) {
        Boolean hasRepeatPermToken = permissionDao.checkRepeatPermToken(permissionToken);
        Boolean notEquOldPermToken = !(permissionToken.equals(oldPermissionToken));
        return !(hasRepeatPermToken && notEquOldPermToken);
    }

    @Override
    public int addPermission(Permission permission) {
        return permissionDao.addPermission(permission);
    }

    @Override
    public void editPermission(Permission permission) {
        permissionDao.editPermission(permission);
    }

    @Override
    public void delPermissionDo(String ids) {
        String[] idList = ids.split(",");
        for (String id : idList){
            permissionDao.delPermissionDo(id);
        }
    }

    @Override
    public Permission findPermissionById(String id) {
        return permissionDao.findPermissionById(id);
    }

    @Override
    public Permission findPermissionByPermissionToken(String permissionToken) {
        return permissionDao.findPermissionByPermissionToken(permissionToken);
    }

}
